<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>按需字典加载性能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1, h2 {
            color: #333;
        }
        .test-section {
            margin-bottom: 30px;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 15px;
        }
        .test-section h3 {
            margin: 0 0 15px 0;
            color: #007bff;
        }
        .api-url {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            margin: 10px 0;
        }
        .btn {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        .btn:hover {
            background: #0056b3;
        }
        .btn-success {
            background: #28a745;
        }
        .btn-success:hover {
            background: #1e7e34;
        }
        .btn-warning {
            background: #ffc107;
            color: #212529;
        }
        .btn-warning:hover {
            background: #e0a800;
        }
        .status {
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .status.info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        .performance-metrics {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin: 15px 0;
        }
        .metric-card {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            padding: 15px;
            text-align: center;
        }
        .metric-value {
            font-size: 24px;
            font-weight: bold;
            color: #007bff;
        }
        .metric-label {
            color: #666;
            margin-top: 5px;
        }
        .comparison-table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        .comparison-table th,
        .comparison-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .comparison-table th {
            background: #f2f5f7;
        }
        .dict-items {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 8px;
            margin: 10px 0;
        }
        .dict-item {
            background: #f8f9fa;
            padding: 5px;
            border-radius: 3px;
            border: 1px solid #e9ecef;
            font-size: 12px;
        }
        .dict-item .value {
            font-weight: bold;
            color: #007bff;
        }
        .dict-item .label {
            color: #333;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>按需字典加载性能测试</h1>
        <p>此页面用于测试按需字典加载的性能优化效果</p>
        
        <div class="test-section">
            <h3>1. 完整字典数据加载测试</h3>
            <div class="api-url">
                GET http://localhost:9002/system/sysDict/getAllDictGrouped
            </div>
            <button class="btn" onclick="testFullDictLoading()">测试完整字典加载</button>
            <div id="full-status" class="status" style="display: none;"></div>
            <div id="full-metrics" class="performance-metrics" style="display: none;"></div>
            <div id="full-response" class="response" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>2. 按需字典数据加载测试</h3>
            <div class="api-url">
                GET http://localhost:9002/system/sysDict/getDictGroupedByCodes?dictCodes=002,003,007,0450000001
            </div>
            <button class="btn btn-success" onclick="testOptimizedDictLoading()">测试按需字典加载</button>
            <div id="optimized-status" class="status" style="display: none;"></div>
            <div id="optimized-metrics" class="performance-metrics" style="display: none;"></div>
            <div id="optimized-response" class="response" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>3. 性能对比分析</h3>
            <button class="btn btn-warning" onclick="comparePerformance()">对比性能</button>
            <div id="comparison-results" style="display: none;"></div>
        </div>

        <div class="test-section">
            <h3>4. 方法学管理页面模拟</h3>
            <button class="btn btn-success" onclick="simulateMethodManagerOptimized()">模拟优化后的方法学管理页面</button>
            <div id="method-manager-sim"></div>
        </div>
    </div>

    <script>
        let fullDictData = null;
        let optimizedDictData = null;
        let fullLoadTime = 0;
        let optimizedLoadTime = 0;

        async function testFullDictLoading() {
            const statusDiv = document.getElementById('full-status');
            const metricsDiv = document.getElementById('full-metrics');
            const responseDiv = document.getElementById('full-response');
            
            statusDiv.style.display = 'block';
            statusDiv.className = 'status info';
            statusDiv.textContent = '正在测试完整字典数据加载...';
            
            const startTime = performance.now();
            
            try {
                const response = await fetch('http://localhost:9002/system/sysDict/getAllDictGrouped');
                const data = await response.json();
                
                const endTime = performance.now();
                fullLoadTime = endTime - startTime;
                
                if (response.ok) {
                    statusDiv.className = 'status success';
                    statusDiv.textContent = '完整字典数据加载成功！';
                    fullDictData = data.data;
                    
                    // 显示性能指标
                    metricsDiv.style.display = 'grid';
                    metricsDiv.innerHTML = `
                        <div class="metric-card">
                            <div class="metric-value">${fullLoadTime.toFixed(2)}ms</div>
                            <div class="metric-label">加载时间</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${data.data ? data.data.length : 0}</div>
                            <div class="metric-label">字典分组数</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${data.data ? data.data.reduce((sum, group) => sum + (group.items ? group.items.length : 0), 0) : 0}</div>
                            <div class="metric-label">总字典项数</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${(JSON.stringify(data).length / 1024).toFixed(2)}KB</div>
                            <div class="metric-label">数据大小</div>
                        </div>
                    `;
                } else {
                    statusDiv.className = 'status error';
                    statusDiv.textContent = `加载失败: ${data.msg || '未知错误'}`;
                }
                
                responseDiv.style.display = 'block';
                responseDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
            } catch (error) {
                statusDiv.className = 'status error';
                statusDiv.textContent = `请求失败: ${error.message}`;
                responseDiv.style.display = 'block';
                responseDiv.innerHTML = error.toString();
            }
        }

        async function testOptimizedDictLoading() {
            const statusDiv = document.getElementById('optimized-status');
            const metricsDiv = document.getElementById('optimized-metrics');
            const responseDiv = document.getElementById('optimized-response');
            
            statusDiv.style.display = 'block';
            statusDiv.className = 'status info';
            statusDiv.textContent = '正在测试按需字典数据加载...';
            
            const startTime = performance.now();
            
            try {
                const dictCodes = ['002', '003', '007', '0450000001'];
                const url = `http://localhost:9002/system/sysDict/getDictGroupedByCodes?dictCodes=${dictCodes.join(',')}`;
                
                const response = await fetch(url);
                const data = await response.json();
                
                const endTime = performance.now();
                optimizedLoadTime = endTime - startTime;
                
                if (response.ok) {
                    statusDiv.className = 'status success';
                    statusDiv.textContent = '按需字典数据加载成功！';
                    optimizedDictData = data.data;
                    
                    // 显示性能指标
                    metricsDiv.style.display = 'grid';
                    metricsDiv.innerHTML = `
                        <div class="metric-card">
                            <div class="metric-value">${optimizedLoadTime.toFixed(2)}ms</div>
                            <div class="metric-label">加载时间</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${data.data ? data.data.length : 0}</div>
                            <div class="metric-label">字典分组数</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${data.data ? data.data.reduce((sum, group) => sum + (group.items ? group.items.length : 0), 0) : 0}</div>
                            <div class="metric-label">总字典项数</div>
                        </div>
                        <div class="metric-card">
                            <div class="metric-value">${(JSON.stringify(data).length / 1024).toFixed(2)}KB</div>
                            <div class="metric-label">数据大小</div>
                        </div>
                    `;
                } else {
                    statusDiv.className = 'status error';
                    statusDiv.textContent = `加载失败: ${data.msg || '未知错误'}`;
                }
                
                responseDiv.style.display = 'block';
                responseDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
            } catch (error) {
                statusDiv.className = 'status error';
                statusDiv.textContent = `请求失败: ${error.message}`;
                responseDiv.style.display = 'block';
                responseDiv.innerHTML = error.toString();
            }
        }

        function comparePerformance() {
            const comparisonDiv = document.getElementById('comparison-results');
            
            if (!fullDictData || !optimizedDictData) {
                comparisonDiv.innerHTML = '<div class="status error">请先完成完整字典和按需字典的加载测试</div>';
                comparisonDiv.style.display = 'block';
                return;
            }
            
            const fullDictCount = fullDictData.reduce((sum, group) => sum + (group.items ? group.items.length : 0), 0);
            const optimizedDictCount = optimizedDictData.reduce((sum, group) => sum + (group.items ? group.items.length : 0), 0);
            
            const timeImprovement = ((fullLoadTime - optimizedLoadTime) / fullLoadTime * 100).toFixed(1);
            const dataReduction = ((fullDictCount - optimizedDictCount) / fullDictCount * 100).toFixed(1);
            
            comparisonDiv.innerHTML = `
                <h4>性能对比结果</h4>
                <table class="comparison-table">
                    <tr>
                        <th>指标</th>
                        <th>完整字典加载</th>
                        <th>按需字典加载</th>
                        <th>优化效果</th>
                    </tr>
                    <tr>
                        <td>加载时间</td>
                        <td>${fullLoadTime.toFixed(2)}ms</td>
                        <td>${optimizedLoadTime.toFixed(2)}ms</td>
                        <td style="color: ${timeImprovement > 0 ? 'green' : 'red'}">${timeImprovement > 0 ? '+' : ''}${timeImprovement}%</td>
                    </tr>
                    <tr>
                        <td>字典分组数</td>
                        <td>${fullDictData.length}</td>
                        <td>${optimizedDictData.length}</td>
                        <td style="color: green">-${fullDictData.length - optimizedDictData.length}</td>
                    </tr>
                    <tr>
                        <td>字典项总数</td>
                        <td>${fullDictCount}</td>
                        <td>${optimizedDictCount}</td>
                        <td style="color: green">-${fullDictCount - optimizedDictCount} (${dataReduction}%)</td>
                    </tr>
                </table>
                
                <div class="status ${timeImprovement > 0 ? 'success' : 'info'}">
                    <strong>优化总结：</strong>
                    ${timeImprovement > 0 ? 
                        `按需加载比完整加载快 ${timeImprovement}%，减少了 ${dataReduction}% 的数据传输量` : 
                        '按需加载的性能提升不明显，可能需要进一步优化'
                    }
                </div>
            `;
            comparisonDiv.style.display = 'block';
        }

        function simulateMethodManagerOptimized() {
            const displayDiv = document.getElementById('method-manager-sim');
            
            if (!optimizedDictData) {
                displayDiv.innerHTML = '<div class="status error">请先完成按需字典加载测试</div>';
                return;
            }

            let html = '<div class="test-section">';
            html += '<h3>优化后的方法学管理页面模拟</h3>';
            html += '<p>只加载必要的字典数据，提高页面加载速度</p>';
            
            // 显示加载的字典类型
            html += '<div style="margin: 20px 0;">';
            html += '<h4>加载的字典类型：</h4>';
            html += '<ul>';
            optimizedDictData.forEach(group => {
                html += `<li><strong>${group.dictName}</strong> (${group.dictCode}): ${group.items ? group.items.length : 0} 项</li>`;
            });
            html += '</ul>';
            html += '</div>';
            
            // 模拟筛选条件
            html += '<div style="margin: 20px 0;">';
            html += '<h4>筛选条件：</h4>';
            
            optimizedDictData.forEach(group => {
                const dictName = group.dictName;
                const items = group.items || [];
                
                html += '<div style="margin: 10px 0;">';
                html += `<strong>${dictName}：</strong>`;
                html += '<select style="margin-left: 10px; padding: 5px;">';
                html += '<option value="">全部</option>';
                items.forEach(item => {
                    html += `<option value="${item.value}">${item.name}</option>`;
                });
                html += '</select>';
                html += '</div>';
            });
            
            html += '</div>';
            
            // 显示性能优势
            html += '<div style="margin: 20px 0;">';
            html += '<h4>性能优势：</h4>';
            html += '<ul>';
            html += '<li>只加载4个必要的字典类型，而不是全部字典</li>';
            html += '<li>减少了网络传输的数据量</li>';
            html += '<li>提高了页面加载速度</li>';
            html += '<li>降低了服务器负载</li>';
            html += '</ul>';
            html += '</div>';
            
            html += '</div>';
            displayDiv.innerHTML = html;
        }

        // 页面加载时显示说明
        window.onload = function() {
            console.log('按需字典加载性能测试页面已加载');
        };
    </script>
</body>
</html>
